<?php
session_start();
require_once("database.php");
$link = $conn=mysql_link();

if ($_GET["action"] == "login"){
    $username = $_POST["email"];
    $password = $_POST["password"];
    $emailErr = "/^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,})$/";
    $passwordErr ="/^[A-Za-z0-9]{6,16}$/";

    $sql = "SELECT * FROM users WHERE email = '$username' and password = '$password'";
    $result = mysqli_query($conn , $sql);

    if ($result->num_rows > 0) {
        $_SESSION['email'] = $username;
        echo "<script>alert('登录成功')</script>";
        echo "<script> location.href = 'return.php'</script>";
    } else {
        echo "<script>alert('用户名或密码错误！')</script>";
    }
}
?>



<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>登录</title>
    <link rel="stylesheet" href="../css/login.css">
</head>
<body>
<div class="all">
    <div class="left">
        <form action="?action=login" class="login_form" method="post">
            <div class="top">
                <div class="wenb"><b>欢迎登录</b></div>
            </div>
            <div class="banner">
                <div class="log-all">
                    <input class="log" type="text" name="email" placeholder="请输入手机号/邮箱">
                    <input class="pass" type="password" placeholder="请输入密码"  name="password">
                </div>
                <div class="foget_word">
                    <a href="#">忘记密码？</a>
                </div>

                <input type="submit" class="login_bottom" value="登录">

                <div class="regi"><p>没有账号？<a href="#">点击注册</a></div>

                <div class="Login_method">
                    <div class="method">
                        <a href="#"><img src="../image/wx.png" alt=""></a>
                        <a href="#"><img src="../image/qq.png" alt=""></a>
                        <a href="#"><img src="../image/更多.png" alt=""></a>
                    </div>
                </div>
            </div>
        </form>
    </div>
    <div class="right">
        <img src="../image/右边.jpg" alt="">
    </div>

</div>
</body>
</html>

<script>
    window.onload = function () {
        var Login_form = document.getElementsByClassName("login_form")[0];
        var Email = document.getElementsByName("email")[0];
        var Password = document.getElementsByName("password")[0];
        //正则
        var email_zz = /^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,})$/;
        var password_zz = /^[A-Za-z0-9]{6,16}$/;
        //账号
        Login_form.onsubmit = function () {
            if(Email.value == ""){
                alert("账号不得为空");
                return false;
            }else{
                if(!email_zz.test(Email.value)){
                    alert("密码格式错误");
                    return false;
                }
            }
            //密码
            if(Password.value == ""){
                alert("密码不得为空");
                return  false
            }else{
                if(!password_zz.test(Password.value)){
                    alert("密码格式错误,须为6,16位");
                    return false;
                }
            }

        }
    }
</script>